<?php
/**
 * 同步考勤id到hroa user表，根据员工编号对应
 * 因为考勤的user_id在别的系统中使用，所有修改hroa中的user_id
 *  10013 | 魏超    | SUZF4371  |
 *   7765 | 魏超    | SUZF4371
 */
apf_require_class('Oa_Job_Abstract');
class Oa_Job_Hr_TmpSyncUserId extends Oa_Job_Abstract {
    /** 考勤系统写 **/
    private $kq_writedb = "attendance_master";
    private $hr_writedb = "hroa_master";

    public function run($params){
        $this->log("开始查询...");

        if($params==1){//检测有多少人的id不一样
            $hr_sql = "SELECT user_id,user_name,user_code FROM user where 1";
            $tmp_hr_datalist=$this->get_rows($hr_sql,array(),false,$this->hr_writedb);

            $kq_sql = "SELECT user_id,name,code FROM user_info WHERE 1 ";
            $kq_datalist=$this->get_rows($kq_sql,array(),false,$this->kq_writedb);

            foreach($tmp_hr_datalist as $val){
                $hr_datalist[$val['user_code']] = $val['user_id'];
            }

            foreach($kq_datalist as $val){
                if(empty($val['code'])){
                    continue;
                }
                $uid = $hr_datalist[$val['code']];
                if($val['user_id'] !=$uid){
                    $this->log($val['name'].'===='.$val['code'].'=Kq='.$val['user_id'].'==='.$uid);
                }
            }
        }elseif($params==3){
            $user_list = array(
                    7751=>7652,
                    7678=>7693,
                    10004=>7752,
                    10005=>7753,
                    10001=>7754,
                    10002=>7755,
                    10003=>7756,
                    10010=>7757,
                    10006=>7758,
                    10007=>7762,
                    10009=>7759,
                    10008=>7760,
                    10011=>7761,
                    10012=>7764,
                    10014=>7767,
             );
            foreach($user_list as $uid=>$newuid){
                $this->sync_uid($uid,$newuid);
            }
        }elseif($params==4){
            $sql = "SELECT * FROM trip_manager where 1 order by new_dep_id DESC";
            $kq_datalist=$this->get_rows($sql,array(),false,$this->kq_writedb);
            foreach($kq_datalist as $val){
                if($val['new_dep_id']){
                file_put_contents('/tmp/xxxdepe.txt',$val['new_dep_id'].'=>'.$val['department_id'].",\n",FILE_APPEND);
                }
            }
            /*$sql = "SELECT * FROM department where 1";
            $kq_datalist=$this->get_rows($sql,array(),false,$this->kq_writedb);
            $i=1;
            foreach($kq_datalist as $val){
                $sql = "SELECT department_id,city_name,department_name FROM department3 where department_name='{$val['department_name']}' AND city_name='{$val['city_name']}' AND is_del=0";
                $data = $this->get_db_row($sql,$this->kq_writedb,array());
                if(empty($data['department_name'])){
                    echo $depname=$val['city_name'].$val['department_name']."\n";
                    file_put_contents('/tmp/depe.txt',$depname,FILE_APPEND);
                }else{
                    $dep_id= $val['department_id'];
                    $upsql ="UPDATE  `attendance`.`trip_manager` SET  `new_dep_id` =  '{$dep_id}' WHERE  `trip_manager`.`department_id` ={$data['department_id']}";
                    echo $upsql;
                    $this->execute( $upsql,array(), $this->kq_writedb);
                }
                $i++;
            }*/

            /*$sql = "SELECT * FROM department where 1";
            $kq_datalist=$this->get_rows($sql,array(),false,$this->kq_writedb);

            $sql = "SELECT * FROM trip_manager  where new_dep_id >0";
            $kq_datalist2=$this->get_rows($sql,array(),false,$this->kq_writedb);
            $dep_ids=array();
            foreach($kq_datalist2 as $val){
                $dep_ids[$val['new_dep_id']] = $val;
            }
            foreach($kq_datalist as $val){
                if(isset($dep_ids[$val['department_id']])){

                }else{
                     echo $depname=$val['city_name'].$val['department_name']."\n";
                     file_put_contents('/tmp/undepe.txt',$depname,FILE_APPEND);
                }
            }*/

        }else{
           /* $kq_sql = "SELECT user_id,name,code FROM user_info WHERE enter_date>='2012-11-29 00:00:00' ";
            $kq_datalist=$this->get_rows($kq_sql,array(),false,$this->kq_writedb);

            $hr_sql = "SELECT user_id,user_name,user_code FROM user where enter_date>=20121129";
            $tmp_hr_datalist=$this->get_rows($hr_sql,array(),false,$this->hr_writedb);
            if(empty($tmp_hr_datalist)){
                $this->log('Not Found User From hroa.user');
            }
            $hr_datalist = array();
            foreach($tmp_hr_datalist as $val){
                $hr_datalist[$val['user_code']] = $val['user_id'];
            }
            foreach($kq_datalist as $val){
                $newuid = $val['user_id'];
                $uid = $hr_datalist[$val['code']];
                $this->sync_uid($uid,$newuid);
            }*/
        }
        $this->log('Done');
    }
    /**
     * 同步1129号之后user_id
     * @param int $uid
     * @param int $newuid
     */
    private function sync_uid($uid,$newuid){
        $upsql = "update user set user_id={$newuid} where user_id={$uid}";
        $this->execute( $upsql,array(), $this->hr_writedb);

        $upsql2 = "update work set user_id={$newuid} where user_id={$uid}";
        $this->execute( $upsql2,array(), $this->hr_writedb);

        $upsql3 = "update unusual_log set user_id={$newuid} where user_id={$uid}";
        $this->execute( $upsql3,array(), $this->hr_writedb);

        $upsql4 = "update relation set user_id={$newuid} where user_id={$uid}";
        $this->execute( $upsql4,array(), $this->hr_writedb);

        $upsql5 = "update relation set parent_id={$newuid} where parent_id={$uid}";
        $this->execute( $upsql5,array(), $this->hr_writedb);

        $upsql6 = "update  join_log set user_id={$newuid} where user_id={$uid}";
        $this->execute( $upsql6,array(), $this->hr_writedb);
        return true;
    }
}